﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using eSYSU.Data.DataAccess;
using eSYSU.Web.Entity;


namespace eSYSU.Web.Business
{
	/// <summary>
	/// 评论服务业务类
	/// </summary>
	public class CommentsService
	{
		public CommentsService()
		{
			//
			//TODO: 在此处添加构造函数逻辑
			//
		}


		public static DataTable GetCommentsByNewsID(int newsID)
		{
			IDictionary<string, object> parms = new Dictionary<string, object>();
			parms.Add("@NewsID", newsID);
			parms.Add("@UserName", null);
			parms.Add("@CommentID", null);

			return SqlHelper.GetDataTable(SqlHelper.ConnString, CommandType.StoredProcedure, "proc_Comment_Get", parms);
		}


		public static Comments GetCommentByID(int CommentID)
		{
			IDictionary<string, object> parms = new Dictionary<string, object>();
			parms.Add("@CommentID", CommentID);
			parms.Add("@UserName", null);
			parms.Add("@NewsID", null);

			DataTable dt = SqlHelper.GetDataTable(SqlHelper.ConnString, CommandType.StoredProcedure, "proc_Comment_Get", parms);

			return null;
		}

		/// <summary>
		/// 根据新闻ID统计评论总数
		/// </summary>
		/// <param name="newsID">新闻ID</param>
		/// <returns>返回该新闻的评论总数</returns>
		public static int GetCommentCount(int newsID)
		{
			IDictionary<string, object> parms = new Dictionary<string, object>();
			parms.Add("@NewsID", newsID);
			parms.Add("@UserName", null);

			return Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnString, CommandType.StoredProcedure, "proc_Comment_Count", parms));
		}


		/// <summary>
		/// 根据用户名统计该用户的评论总数
		/// </summary>
		/// <param name="username">用户名</param>
		/// <returns>返回该用户的评论总数</returns>
		public static int GetCommentCount(string username)
		{
			IDictionary<string, object> parms = new Dictionary<string, object>();
			parms.Add("@UserName", username);
			parms.Add("@NewsID", null);

			return Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnString, CommandType.StoredProcedure, "proc_Comment_Count", parms));
		}


		public static DataTable GetCommentByUsername(string username)
		{
			IDictionary<string, object> parms = new Dictionary<string, object>();
			parms.Add("@UserName", username);
			parms.Add("@NewsID", null);
			parms.Add("@CommentID", null);

			return SqlHelper.GetDataTable(SqlHelper.ConnString, CommandType.StoredProcedure, "proc_Comment_Get", parms);
		}


		public static DataTable GetHotCommentNews(int newsCount)
		{
			IDictionary<string, object> parms = new Dictionary<string, object>();
			parms.Add("@newsCount", newsCount);

			return SqlHelper.GetDataTable(SqlHelper.ConnString, CommandType.StoredProcedure, "proc_News_Hot_Comment", parms);
		}


		/// <summary>
		/// 插入一条新闻评论
		/// </summary>
		/// <param name="comment">评论</param>
		/// <returns></returns>
		public static object InserNewsComment(Comments comment)
		{
			IDictionary<string, object> parmsDic = new Dictionary<string, object>();
			parmsDic.Add("@newsid", comment.ArticleId);
			parmsDic.Add("@username", comment.UserName);
			parmsDic.Add("@posttime", comment.PostTime);
			parmsDic.Add("@content", comment.Comment);

			object obj = SqlHelper.ExecuteScalar(SqlHelper.ConnString, CommandType.StoredProcedure, "proc_Comment_Insert", parmsDic);
			return obj;
		}


		/// <summary>
		/// 将XML字符串格式化为NewsComment对象
		/// </summary>
		/// <param name="xmlComment">XML字符串</param>
		/// <returns>NewsComment对象</returns>
		public static Comments LoadXml(string xmlComment)
		{
			Comments comment = new Comments();

			System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
			xd.LoadXml(xmlComment);

			comment.Id = Convert.ToInt32(xd.SelectSingleNode("//ID").InnerText);
			comment.ArticleId = Convert.ToInt32(xd.SelectSingleNode("//NewsID").InnerText);
			comment.UserName = xd.SelectSingleNode("//Username").InnerText;
			comment.PostTime = DateTime.Now;
			comment.Comment = xd.SelectSingleNode("//Content").InnerText;

			return comment;
		}


	}
}